package com.warm.androidstudy.utils;

import android.text.InputFilter;
import android.text.Spanned;

/**
 * <pre>
 *     author : guosenlin
 *     e-mail : guosenlin91@gmail.com
 *     time   : 2021/04/16
 *     desc   :
 *     version: 1.0
 * </pre>
 */

public class NumberDecimalFilter implements InputFilter {
    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {

        if (source.equals(".") && dest.length() == 0){
            return "0.";
        }

        if (dest.toString().contains(".")) {
            int index = dest.toString().indexOf(".");
            int length = dest.toString().substring(index).length();
            if (length == 3) {
                return "";
            }
        }
        if (dest.toString().equals("0") && source.equals("0")){
            return "";
        }
        return null;
    }
}
